home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / ABUSESRC.ZIP / AbuseSrc / abuse / src / net / mac / tcpip.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-07  |  6.6 KB  |  233 lines

  1. #include "sock.hpp"
  2.  
  3. #include <stdlib.h>
  4. #include <fcntl.h>
  5. #include <sys/ioctl.h>
  6. #include <sys/stat.h>
  7. #include <sys/types.h>
  8. #include <signal.h>
  9. #include <sys/types.h>
  10. #include "isllist.hpp"
  11.  
  12. #ifdef __POWERPC__
  13. #include "GUSI.h"
  14. #else
  15. #include <netdb.h>
  16. #include <netinet/in.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <sys/time.h>
  20. #include <sys/ipc.h>
  21. #include <sys/shm.h>
  22. #include <sys/socket.h>
  23. #include <unistd.h>
  24. #ifdef HAVE_BSTRING_H
  25. #include <bstring.h>
  26. #else
  27. #include <sys/select.h>
  28. #endif
  29. #endif
  30.  
  31. extern fd_set master_set,master_write_set,read_set,exception_set,write_set;
  32.  
  33. class ip_address : public net_address
  34. {
  35. public:
  36.   sockaddr_in addr;
  37.  
  38.   virtual protocol protocol_type() const { return net_address::IP; }
  39.   virtual int equal(const net_address *who) const
  40.   //{{{
  41.   {
  42.     if (who->protocol_type()==IP &&
  43.         !memcmp(&addr.sin_addr,& ((ip_address *)who)->addr.sin_addr,sizeof(addr.sin_addr)))
  44.       return 1;
  45.     else return 0;
  46.   }
  47.     //}}}
  48.   virtual int set_port(int port)  { addr.sin_port=htons(port); return 1; }
  49.   ip_address(sockaddr_in *Addr) { memcpy(&addr,Addr,sizeof(addr)); }
  50.   virtual void print()
  51.   //{{{
  52.   {
  53.     unsigned char *c=(unsigned char *) (&addr.sin_addr.s_addr);
  54.     fprintf(stderr,"%d.%d.%d.%d",c[0],c[1],c[2],c[3]);
  55.   }
  56.     //}}}
  57.   int get_port() { return htons(addr.sin_port); }
  58.   net_address *copy()  { return new ip_address(&addr); }
  59.   ip_address() {} ;
  60.   void store_string(char *st, int st_length)
  61.   //{{{
  62.   {
  63.     char buf[100];
  64.     unsigned char *c=(unsigned char *) (&addr.sin_addr.s_addr);
  65.     sprintf(buf,"%d.%d.%d.%d:%d",c[0],c[1],c[2],c[3],htons(addr.sin_port));
  66.     strncpy(st,buf,st_length);
  67.     st[st_length-1]=0;    
  68.   }
  69.   //}}}
  70. } ;
  71.  
  72. class tcpip_protocol : public net_protocol
  73. {
  74. protected:
  75.   // Request Data
  76.   struct RequestItem
  77.   {
  78.       ip_address *addr;
  79.       char name[256];   //name
  80.   };
  81.   typedef isllist<RequestItem *>::iterator p_request;
  82.   isllist<RequestItem*> servers,returned;
  83.   net_socket *responder;
  84.   ip_address *bcast;
  85.  
  86.   // Notification Data
  87.   net_socket *notifier;
  88.   char notify_data[512];
  89.   int notify_len;
  90.  
  91.   int handle_notification();
  92.   int handle_responder();
  93. public :
  94.   fd_set master_set,master_write_set,read_set,exception_set,write_set;
  95.  
  96.   tcpip_protocol();
  97.   net_address *get_local_address();
  98.   net_address *get_node_address(char *&server_name, int def_port, int force_port);
  99.   net_socket *connect_to_server(net_address *addr, 
  100.         net_socket::socket_type sock_type=net_socket::SOCKET_SECURE);
  101.   net_socket *create_listen_socket(int port, net_socket::socket_type sock_type);
  102.   int installed() { return 1; }  // always part of unix
  103.   char *name() { return "UNIX generic TCPIP"; }
  104.   void cleanup(); 
  105.   int select(int block);          // return # of sockets available for read & writing
  106.   
  107.   // Notification methods
  108.   virtual net_socket *start_notify(int port, void *data, int len);
  109.   virtual void end_notify();
  110.  
  111.   // Find notifiers methods
  112.   virtual net_address *find_address(int port, char *name);   // name should be a 256 byte buffer
  113.   virtual void reset_find_list();
  114.   virtual ~tcpip_protocol() { cleanup(); }
  115. } ;
  116.  
  117. extern tcpip_protocol tcpip;
  118.  
  119. class unix_fd : public net_socket
  120. {
  121.   protected :
  122.   int fd;
  123.   public :
  124.   unix_fd(int fd) : fd(fd) { };
  125.   virtual int error()                             { return FD_ISSET(fd,&tcpip.exception_set); }
  126.   virtual int ready_to_read()                     { return FD_ISSET(fd,&tcpip.read_set); }
  127.   virtual int ready_to_write()                    
  128.   { 
  129.     struct timeval tv={0,0};     // don't wait
  130.     fd_set write_check;  
  131.     FD_ZERO(&write_check);  
  132.     FD_SET(fd,&write_check);     
  133.     select(FD_SETSIZE,NULL,&write_check,NULL,&tv);
  134.     return FD_ISSET(fd,&write_check); 
  135.   }
  136.   virtual int write(void *buf, int size, net_address *addr=NULL);
  137.   virtual int read(void *buf, int size, net_address **addr);
  138.  
  139.   virtual ~unix_fd()                            { read_unselectable();  write_unselectable(); close(fd); }
  140.   virtual void read_selectable()                   { FD_SET(fd,&tcpip.master_set); }
  141.   virtual void read_unselectable()                 { FD_CLR(fd,&tcpip.master_set); }
  142.   virtual void write_selectable()                  { FD_SET(fd,&tcpip.master_write_set); }
  143.   virtual void write_unselectable()                { FD_CLR(fd,&tcpip.master_write_set); }
  144.   int get_fd() { return fd; }
  145.   
  146.   void broadcastable();
  147. } ;
  148.  
  149. class tcp_socket : public unix_fd
  150. {
  151.   int listening;
  152.   public :
  153.   tcp_socket(int fd) : unix_fd(fd) { listening=0; };
  154.   virtual int listen(int port)
  155.   {
  156.     sockaddr_in host;
  157.     memset( (char*) &host,0, sizeof(host));
  158.     host.sin_family = AF_INET;
  159.     host.sin_port = htons(port);
  160.     host.sin_addr.s_addr = htonl(INADDR_ANY);
  161.     if (bind(fd, (struct sockaddr *) &host, sizeof(sockaddr_in))==-1)
  162.     {
  163.       fprintf(stderr,"net driver : could not bind socket to port %d\n",port);
  164.       return 0;
  165.     }
  166.     if (::listen(fd,5)==-1)
  167.     {
  168.       fprintf(stderr,"net driver : could not listen to socket on port %d\n",port);    
  169.       return 0;
  170.     }
  171.     listening=1;
  172.     return 1;
  173.   }
  174.   virtual net_socket *accept(net_address *&addr) 
  175.   { 
  176.     if (listening)
  177.     {
  178.       struct sockaddr_in from;
  179.       int addr_len=sizeof(from);
  180.       int new_fd=::accept(fd,(sockaddr *)&from,&addr_len);
  181.       if (new_fd>=0)
  182.       {
  183.         addr=new ip_address(&from);
  184.         return new tcp_socket(new_fd);
  185.       }
  186.       else 
  187.       { addr=NULL; return 0; }
  188.     }
  189.     return 0;
  190.   }
  191. } ;
  192.  
  193. class udp_socket : public unix_fd
  194. {
  195.   public :
  196.   udp_socket(int fd) : unix_fd(fd) { };
  197.   virtual int read(void *buf, int size, net_address **addr)
  198.   {
  199.     int tr;
  200.     if (addr) 
  201.     {
  202.       *addr=new ip_address;
  203.       int addr_size=sizeof(sockaddr_in);
  204.       tr=recvfrom(fd,buf,size,0, (sockaddr *) &((ip_address *)(*addr))->addr,&addr_size);
  205.     } else
  206.       tr=recv(fd,buf,size,0);
  207.     return tr;
  208.   }
  209.   virtual int write(void *buf, int size, net_address *addr=NULL)
  210.   {
  211.     if (addr)
  212.       return sendto(fd,buf,size,0,(sockaddr *)(&((ip_address *)addr)->addr),sizeof(((ip_address *)addr)->addr));
  213.     else 
  214.       return ::write(fd,(char*)buf,size);     
  215.   }
  216.   virtual int listen(int port)
  217.   {
  218.     sockaddr_in host;
  219.     memset( (char*) &host,0, sizeof(host));
  220.     host.sin_family = AF_INET;
  221.     host.sin_port = htons(port);
  222.     host.sin_addr.s_addr = htonl(INADDR_ANY);
  223.     if (bind(fd, (struct sockaddr *) &host, sizeof(sockaddr_in))==-1)
  224.     {
  225.       fprintf(stderr,"net driver : could not bind socket to port %d\n",port);
  226.       return 0;
  227.     }
  228.     return 1;
  229.   }
  230.  
  231. } ;
  232.  
  233.